package cn.funeralobjects.upms.converter;

import cn.funeralobjects.cache.EnumCache;
import cn.funeralobjects.upms.enums.HttpMethod;

import javax.persistence.AttributeConverter;

/**
 * HttpMethod的转换器
 *
 * @author FuneralObjects
 * Create date: 2020/4/26 10:56 AM
 */
public class HttpMethodConverter implements AttributeConverter<HttpMethod, String> {
    @Override
    public String convertToDatabaseColumn(HttpMethod attribute) {
        return attribute == null ? null : attribute.name();
    }

    @Override
    public HttpMethod convertToEntityAttribute(String dbData) {
        HttpMethod result = EnumCache.of(dbData, HttpMethod.class);
        if (result != null || EnumCache.isRegistered(HttpMethod.class)) {
            return result;
        }
        EnumCache.registerCache(HttpMethod.class, HttpMethod::name);
        return EnumCache.of(dbData, HttpMethod.class);
    }
}
